//index.js
//获取应用实例
const app = getApp();
const getDateList = require('../../utils/date.js');
const Format = require("../../utils/time.js");
var getList = function(that, newDate) { // 活动列表
    app.getApi.getProducts("POST", {
        pageNumber: that.data.pageNumber,
        pageSize: 10,
        availableBuyTime: newDate,
        isNotEndNow: 1,
        cityId: that.data.cityId,
        method: "opg.eventPromote.find"
    }).then(data => {
        var list = that.data.items;
        if (data.data.totalCount == 0) {
            that.setData({
                noData: true
            })
        }
        // if (data.data.code == 200 && data.data.errMsg == "request:ok") {
        for (let i = 0; i < data.data.result.length; i++) {
            let dates = data.data.result[i].startTimeStr;
            var testTime = Format.getMonth(parseInt(dates.substring(5, 7)), that.data.isChinese)

            let times = testTime + dates.substring(8, 10);
            data.data.result[i].startTimeStr = times
            list.push(data.data.result[i])
        }
        if (Math.ceil(data.data.totalCount / 10) == that.data.pageNumber) {
            that.setData({
                isAll: true
            })
        }
        that.setData({
            items: list,
            loadMore: false
        })
    })
}
Page({
    data: {
        indexs: 0,
        pageNumber: 1,
        isNotEndNow: 1,
        currentNavs: { name: "Party", list: app.globalData.navs },
        areaName: "洛杉矶",
        years: [],
        hidden: false, //没有更多
        loadMore: true, //是否显示加载更多
        isAll: false,
        items: [],
        isChinese: wx.getStorageSync("isChinese"),
        code: "",
        userInfo: {},
        cityName: "",
        cityId: ""
    },
    //事件处理函数
    bindViewTap: function() {

    },

    onLoad: function() {
        var res = wx.getSystemInfoSync();
        this.setData({
            eventWidth: res.windowWidth - 77
        })
        if (wx.getStorageSync("areaName")) {
            this.setData({
                cityName: wx.getStorageSync("areaName"),
                cityId: wx.getStorageSync("areaId")
            })

        }
        if (getApp().getApi.session == null || getApp().getApi.session == "") {
            getApp().watch = this.initData
        } else {
            this.initData()
        }

    },
    initData() {
        var that = this
        that.setData({
            isChinese: wx.getStorageSync("isChinese")
        })
        wx.setNavigationBarTitle({
            title: that.data.isChinese ? "搜索结果" : "Search Results"
        })
        var years = wx.getStorageSync("years");
        var selecDate = [];
        var selecDateList = [];
        for (let i = 0; i < years.length; i++) {

            if (years[i].select) {
                selecDateList.push(years[i])
                selecDate.push(years[i].year + '-' + years[i].monthN + '-' + years[i].day + " 00:00:00")
            }
        }
        that.setData({
            selecDate: selecDate,
            selecDateList: selecDateList
        })
        var newDate = selecDate.join(",")

        getList(that, newDate)
    },
    // getDates() {
    //     let newDate = Format.Format()
    //     var params = {
    //         startDate: newDate,
    //         method: "opg.mobile.evendatalist.find"
    //     }
    //     app.getApi.getProducts("POST", params).then(res => {})
    // },
    onShow() {},
    getUserInfo: function(e) { // 获取用户信息
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    selectDate(e) { //切换活动日期
        var that = this
        let year = e.currentTarget.dataset.year,
            month = e.currentTarget.dataset.month,
            monthN = e.currentTarget.dataset.monthn,
            day = e.currentTarget.dataset.day,
            nowToday = year + "年" + month + "月" + day + "日",
            newDate = year + "-" + monthN + "-" + day + " 00:00:00";
        that.setData({
            selecDate: nowToday,
            pageNumber: 1,
            items: [],
            hidden: false,
            loadMore: false,
            isAll: false,
            isNotEndNow: 1,
        })
        getList(that, newDate)
    },
    lookImg(e) {
        let imgList = e.currentTarget.dataset.imglist
        wx.previewImage({
            current: imgList[0], // 当前显示图片的http链接
            urls: imgList // 需要预览的图片http链接列表
        })
    },
    city() {
        wx.navigateTo({
            url: '../country/country?isTpe=1',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    detail(e) { //查看详情
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../activityDetails/activityDetails?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    onReachBottom() { //触底事件
        var that = this
        if (!that.data.isAll) {
            that.setData({
                pageNumber: that.data.pageNumber + 1,
                loadMore: true

            })
            getList(that)
        } else {
            that.setData({
                hidden: true
            })
        }
    },
    dateSelect() { //选择日期
        wx.navigateTo({
            url: '../dateSelect/dateSelect',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    }
})